#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    create();
    //给表格插入3行3列
    table_widget->insertRow(0);
    table_widget->insertRow(1);
    table_widget->insertRow(2);

    table_widget->insertColumn(0);
    table_widget->insertColumn(1);
    table_widget->insertColumn(2);

    //给列添加列名称
    table_widget->setHorizontalHeaderItem(0,new QTableWidgetItem("学号"));
    table_widget->setHorizontalHeaderItem(1,new QTableWidgetItem("姓名"));
    table_widget->setHorizontalHeaderItem(2,new QTableWidgetItem("年龄"));

    //给这个表格中插入信息
    table_widget->setItem(0,0,new QTableWidgetItem("0001"));
    table_widget->setItem(0,1,new QTableWidgetItem("张三"));
    table_widget->setItem(0,2,new QTableWidgetItem("12"));

    table_widget->setItem(1,0,new QTableWidgetItem("0002"));
    table_widget->setItem(1,1,new QTableWidgetItem("李四"));
    table_widget->setItem(1,2,new QTableWidgetItem("14"));

    table_widget->setItem(2,0,new QTableWidgetItem("0003"));
    table_widget->setItem(2,1,new QTableWidgetItem("王五"));
    table_widget->setItem(2,2,new QTableWidgetItem("18"));

    //为按钮实现功能
    connect(add_row,&QPushButton::clicked,this,&Widget::handler_add_row);
    connect(sub_row,&QPushButton::clicked,this,&Widget::handler_sub_row);
    connect(add_column,&QPushButton::clicked,this,&Widget::handler_add_column);
    connect(sub_column,&QPushButton::clicked,this,&Widget::handler_sub_column);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::create()
{
    table_widget=new QTableWidget(this);
    table_widget->setGeometry(50,50,500,400);

    add_row=new QPushButton(this);
    add_row->setText("新增一行");
    add_row->setGeometry(50,500,80,50);

    sub_row=new QPushButton(this);
    sub_row->setText("删除一行");
    sub_row->setGeometry(50,560,80,50);

    add_column=new QPushButton(this);
    add_column->setText("新增一列");
    add_column->setGeometry(150,500,80,50);

    sub_column=new QPushButton(this);
    sub_column->setText("删除一列");
    sub_column->setGeometry(150,560,80,50);

    line_edit=new QLineEdit(this);
    line_edit->setGeometry(300,520,300,50);
    line_edit->setPlaceholderText("请输入列名称");

}

void Widget::handler_add_row()
{
    //新增一行
    //获取表格行数
    int rowCount=table_widget->rowCount();
    //插入一行
    table_widget->insertRow(rowCount);
}

void Widget::handler_sub_row()
{
    //删除一行
    //获取选中单元格的下标
    int rowCount=table_widget->currentRow();
    //插入一行
    table_widget->removeRow(rowCount);
}

void Widget::handler_add_column()
{
    //新增一列
    //获取列数
    int columnCount=table_widget->columnCount();
    //插入列
    table_widget->insertColumn(columnCount);
    //读取输入框中列名称
    const QString str=line_edit->text();
    //设置列名称
    table_widget->setHorizontalHeaderItem(columnCount,new QTableWidgetItem(str));
    //清除输入框中内容
    line_edit->clear();
}

void Widget::handler_sub_column()
{
    //删除选中列
    //获取选中的列的下标
    int curCol=table_widget->currentColumn();
    //删除这一列
    table_widget->removeColumn(curCol);
}

